LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   php: Why Can't I Query Mysql DB?? (https://www.linuxquestions.org/questions/programming-9/php-why-cant-i-query-mysql-db-253889/)

flamesrock 11-11-2004 11:22 PM

php: Why Can't I Query Mysql DB??
 
Ok, I have a database, set up as follows:

It extracts the data from cities contained in a simcity 4 region. The table I'm interested in-- 'regions', in that DB contains the population numbers for an entire region (per row):

id name total_pop total_R total_C total_I vrestrict created modified

1 SimNation_II 2035783 1332966 504280 198537 0 2004-10-22 11:39:39 2004-11-11 03:55:37


Anyways, the code is supposed to connect to mysql, extract these values, and display them, but it returns '0' and NOTHING else for any of the values: http://dyslexitech.com/stats.php

Here is the code:
Code:

<?php

$link = mysql_connect('localhost', 'root', 'blasdfadfs');
if (!$link) {
  die('Not connected : ' . mysql_error());
}

// make simnationII the current db
$db_selected = mysql_select_db('simnationII');
if (!$db_selected) {
  die ('Can\'t use foo : ' . mysql_error());
}
//
$sql = "SELECT * FROM regions";
$region = mysql_query($sql);
//
if (!$region) {
  die('SCORE is unavailable: ' . mysql_error());
}
if ($region) {
 while ($row = mysql_fetch_row($region)) {
  echo "<br>";
  echo "
  <div style=\"margin-bottom: 10px; width: 100%; height: 149\">
    <br>
    - <b>Created</b>: {$region['created']}
    <br>
    - <b>Last Updated</b>: {$region['modified']}
    <p>
    - <b>Total Population</b>: ".number_format($region['total_pop'])."
    <br>
    &nbsp;&nbsp;
    <b>R</b>: ".number_format($region['total_R'])."
    <br>
    &nbsp;&nbsp;
    <b>C</b>: ".number_format($region['total_C'])."
    <br>
    &nbsp;&nbsp;
    <b>I</b>: ".number_format($region['total_I'])."
    <p>
    - <b>Total Funds</b>: $".number_format($region['total_money'])."
    <p>
    <div style=\"width:300px; height: 100px; overflow:auto\">
    ".nl2br($region['description'])."
    </div>
    <p>
    ";
 
  // are we making available zip archives of each region?
 
  // do we have config.bmp and terrain maps for this region?

  // close out the div
    echo "
  </div>
  <br>";
  }
 }
?>

I would greatly appreciate ANY help.

-thanks in advance.

masand 11-11-2004 11:39 PM

hi

ry these out!!

see that the database is runnig and listening on a specific port
for this u can portscan then machine

and try browsing ur database from the shell aslo / or any gui

regards

flamesrock 11-12-2004 12:31 AM

Its running alright :( I checked with phpMyAdmin and also through ssh.

And the mysql port (3306) is open too...

any more suggestions?

-thanks

masand 11-12-2004 12:49 AM

hi there

is this username "root" working fine with PHPmyadmin??
if not try out with blan username and password...

regards

cbe 11-12-2004 08:12 PM

I think I see your problem,


try using: mysql_fetch_array($region) instead of mysql_fetch_row($region)

mysql_fetch_array() Returns an array that corresponds to the fetched row and is an extended version of mysql_fetch_row()

also

try using: {$row['created']} instead of {$region['created']}

remember that $row is the array, not $region.


mysql_fetch_array() creates an array using $row with each value returned from mysql.

see:
http://us2.php.net/manual/en/functio...etch-array.php
http://us2.php.net/manual/en/function.echo.php



Hope this helps...

flamesrock 11-12-2004 09:46 PM

THANKYOU so much! :D That did it.

And I was about to give up :cool:

And thankyou too masand. :) (BTW, root does work in phpMyAdmin)

marks_linux 11-14-2004 03:08 PM

I'd be tempted to set up another mysql user with proper granted permissions pretty soon, and use that for your mysql connection. I have a read-only (select) user and at least one other user with insert etc permissions - save some potential heartache!

cryptwizard 11-16-2004 12:36 AM

I think the is a place where you need to put a MYSQL_ASSOC flag to return an associative array (an array with named elements ($a["b"] as opposed to $a[0])


All times are GMT -5. The time now is 07:51 PM.