Forum     

Go Back   Digit Technology Discussion Forum > Software > Programming
Register FAQ Calendar Mark Forums Read

Programming The destination for developers - C, C++, Java, Python and the lot


Reply
 
LinkBack Thread Tools Display Modes
Old 18-06-2011, 02:04 AM   #1 (permalink)
learn more share more....
 
hacklinux's Avatar
 
Join Date: May 2011
Posts: 36
Post about connecting php and mysql


can you please help me with connecting mysql with php?the code i used is

<?php

# Define MySQL Settings
define("MYSQL_HOST", "localhost");
define("MYSQL_USER", "root");
define("MYSQL_PASS", "hello");
define("MYSQL_DB", "test");

$conn = mysql_connect("".MYSQL_HOST."", "".MYSQL_USER."", "".MYSQL_PASS."") or die(mysql_error());
mysql_select_db("".MYSQL_DB."",$conn) or die(mysql_error());

$sql = "SELECT * FROM test";
$res = mysql_query($sql);

while ($field = mysql_fetch_array($res))
{
$id = $field['id'];
$name = $field['name'];

echo 'ID: ' . $field['id'] . '<br />';
echo 'Name: ' . $field['name'] . '<br /><br />';
}

?>



but it displays an error:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\server\htdocs\mysql_test.php on line 15



please help me at the earliest.
hacklinux is offline   Reply With Quote
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 18-06-2011, 01:26 PM   #2 (permalink)
Alpha Geek
 
Join Date: Feb 2004
Posts: 687
Default Re: about connecting php and mysql

Assuming rest is without any error, you have to give one more parameter to mysql_fetch_array function. Since you are suing associative array in result, it should be
Code:
while ($field = mysql_fetch_array($res,MYSQL_ASSOC))

Let me know if it works for you or else I will test this at my place!
__________________
~Ricky

» Looking for someone to partner with me in Indian Linux forum
Free domain is available ! I am also at Indian Forums.
Ricky is offline   Reply With Quote
Old 20-06-2011, 08:24 PM   #3 (permalink)
learn more share more....
 
hacklinux's Avatar
 
Join Date: May 2011
Posts: 36
Default Re: about connecting php and mysql

no it didnt work.it says d same:



Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\server\htdocs\bic.php on line 15
hacklinux is offline   Reply With Quote
Old 20-06-2011, 08:34 PM   #4 (permalink)
Simply a DIGITian
 
krishnandu.sarkar's Avatar
 
Join Date: Nov 2007
Location: Kolkata
Posts: 2,942
Default Re: about connecting php and mysql

I see few errors there...

Try this...

PHP Code:

<?php

$link 
mysql_connect("localhost""root""root") or die(mysql_error());
mysql_select_db("testdb"$link) or die(mysql_error());

$sql "SELECT * FROM tbltestdb";
$result mysql_query($sql$link);

while (
$row mysql_fetch_array($result))
{
$id $row['id'];
$name $row['name'];

echo 
"ID: " $id "<br />";
echo 
"Name: " $name "<br />";
}

mysql_close($link);
?>
Now the errors I'm suspecting are...

1.

PHP Code:
# Define MySQL Settings
define("MYSQL_HOST""localhost");
define("MYSQL_USER""root");
define("MYSQL_PASS""hello");
define("MYSQL_DB""test");

$conn mysql_connect("".MYSQL_HOST."""".MYSQL_USER."""".MYSQL_PASS."") or die(mysql_error()); 
So the string that's getting genrated is...

PHP Code:
$conn mysql_connect("""localhost""""""root""""""hello""") or die(mysql_error()); 
Because...you wrote

mysql_connect("".MYSQL_HOST.""

so the variable MYSQL_HOST = "localhost" that you defined avobe

so "localhost" gets concatinated as ""."localhost"."" which becomes """localhost"""

Right..??

So you should have write it as...

PHP Code:
$conn mysql_connect(MYSQL_HOSTMYSQL_USERMYSQL_PASS) or die(mysql_error()); 
Better use...

PHP Code:
$dbhost "localhost";
$dbuser "root";
$dbpass "root";

$link mysql_connect($dbhost$dbuser$dbpass); 

Another one is...

2.

PHP Code:
$res mysql_query($sql); 
PHP Code:
mysql_query($sql); 
should actually be
PHP Code:
mysql_query($sql$link); 
Though I've seen many people to write it as like you did. I don't know whether it's right or wrong, but $link should be provided as the function prototype itself says that...

Correct me if I'm wrong anywhere..!!

And lastly one suggestion...always perform mysql_close();

Happy Coding
__________________
  • Read The Forum RULES First.
  • Before PM'ing Or Asking Any Questions To Any Mod Read The FAQ's
  • Before Starting A New Thread Read The STICKY THREADS First
  • Before Participating In Bazaar Section Read The BAZAAR RULES
krishnandu.sarkar is online now   Reply With Quote
Old 21-06-2011, 01:31 PM   #5 (permalink)
Wise Old Owl
 
speedyguy's Avatar
 
Join Date: Aug 2004
Location: Bangalore/Jamshedpur
Posts: 1,173
Default Re: about connecting php and mysql

is it possible for u to use mysql_fetchrow() instead of mysql_fetch_array() ...

Enjoy~!
__________________
No Pain...No Gain
speedyguy is offline   Reply With Quote
Old 25-06-2011, 04:43 PM   #6 (permalink)
XLr8
 
arpanmukherjee1's Avatar
 
Join Date: Sep 2008
Posts: 637
Default Re: about connecting php and mysql

@krishnandu.sarkar
i have been learning PHP and ur code worked out fine.

what is the best option if speed of connectivity and execution is to be considered - mysql mysqli PEAR db or PDO ??? (are the there any more extentions ?)

also how can i find the PHP version other than phpinfo() ??

as PDO is not available for versions <5.0 i want to write a function that takes the DB driver name as parameter and find out which version of PHP is available and will return the connection variable either of PEAR db or PDO type
__________________
Quote:
There are more things in heaven and earth, Horatio,
Than are dreamt of in your philosophy.
arpanmukherjee1 is offline   Reply With Quote
Old 25-06-2011, 08:20 PM   #7 (permalink)
127.0.0.1
 
mitraark's Avatar
 
Join Date: Nov 2010
Location: Kolkata / Durgapur
Posts: 769
Default Re: about connecting php and mysql

Code:
$conn = mysql_connect("".MYSQL_HOST."", "".MYSQL_USER."", "".MYSQL_PASS."") or die(mysql_error());
Why use the "". ... ."" ? Simply write

Code:
$conn = mysql_connect(MYSQL_HOST,MYSQL_USER,MYSQL_PASS)
if(!isset($connection))
die(mysql_error());
__________________
Intel i5 760 || Gigabyte H55 || Corsair 4GB 1333MHz Value || XFX HD5670 1GB
WD 2TB Green + Seagate 5900RPM 2 TB + Seagate Freeagent 1 TB x2
Corsair CX400 || Zebronics Bijli || Samsung P2350 23.5" || Altec Lansing VS2621
My Speedtest :D
mitraark is online now   Reply With Quote
Old 26-06-2011, 11:19 AM   #8 (permalink)
Simply a DIGITian
 
krishnandu.sarkar's Avatar
 
Join Date: Nov 2007
Location: Kolkata
Posts: 2,942
Default Re: about connecting php and mysql

Quote:
Originally Posted by arpanmukherjee1 View Post
@krishnandu.sarkar
i have been learning PHP and ur code worked out fine.

what is the best option if speed of connectivity and execution is to be considered - mysql mysqli PEAR db or PDO ??? (are the there any more extentions ?)

also how can i find the PHP version other than phpinfo() ??

as PDO is not available for versions <5.0 i want to write a function that takes the DB driver name as parameter and find out which version of PHP is available and will return the connection variable either of PEAR db or PDO type
Sorry no idea. I don't know that much PHP, just more than the basics.
__________________
  • Read The Forum RULES First.
  • Before PM'ing Or Asking Any Questions To Any Mod Read The FAQ's
  • Before Starting A New Thread Read The STICKY THREADS First
  • Before Participating In Bazaar Section Read The BAZAAR RULES
krishnandu.sarkar is online now   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


 
Latest Threads
- by Charan
- by Sarath
- by clmlbx

Advertisement




All times are GMT +5.5. The time now is 12:30 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2