Jump to content

[PHP][MySQL] problem with mysql_connect


latino.ad7

Recommended Posts

I have a problem when using mysql_connect and I can't properly connect to the database. I use WebServ. I had it working OK, but now after reinstall, something is wrong and I can't determine what.

 

I used PHPMyAdmin to set up a table called "objects" in database "mydb". I input one sample record.

 

When I run php code I get the following message: Resource id #3. What's wrong??

 

The variable $r (see the code below) doesn't seem to include anything.

 

Here is PHP code:

 

$hostt = 'localhost';

$userr = 'root';

$passw = '';

$baz = 'mydb';

 

$link = mysql_connect ($hostt, $userr, $passw);

$db = mysql_select_db($baz, $link);

 

$q = ("Select * from objects");

$r = mysql_query ($q, $link);

 

echo $r;

Link to comment
https://forums.phpfreaks.com/topic/191793-phpmysql-problem-with-mysql_connect/
Share on other sites

Ah, certainly. Thank you so much. It works! :)

 

How do I process this query correctly?

 

It works ok when I add:

$row = mysql_fetch_array($r);

echo $row[0];

echo $row[1];

 

But it fails to work in a loop:

while ($row = mysql_fetch_array($r)) {

echo $row['name'];

}

 

How do I type that correctly?

 

Let's say that my table "objects" consists of 3 columns: id, name, author.

And I have 2 rows:

0, "aaa", "aaa0"

1, "bbb", "bbb0"

Thanks. That's what I did finally:

 

$hostt = 'localhost';

$userr = 'root';

$passw = '';

$baz = 'mydb';

 

$link = mysql_connect ($hostt, $userr, $passw);

$db = mysql_select_db($baz, $link);

 

$q = ("Select id, filename, name, author, bin from objects where category = 'mycategory');

$r = mysql_query ($q, $link);

 

while ($row = mysql_fetch_array($r, MYSQL_ASSOC)) {

$arrid[$row['id']] = $row['id'];

$arrfilename[$row['id']] = $row['filename'];

$arrname[$row['id']] = $row['name'];

$arrauthor[$row['id']] = $row['author'];

$arrbin[$row['id']] = $row['bin'];

 

echo $arrname[0];

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.