0

I have been trying to do this for a day or two now and just can't understand why it isn't working as I have others set up in the same way.

I just want to simply return some data from a database.

This is linked to a php file (index.php) that just includes it.

<?php
/*connect to local database */
$host="localhost"; // Host name 
$db_username="Josh"; // Mysql username 
$db_password="password"; // Mysql password 
$db_name="messenger"; // Database name 
$tbl_name="messages"; // Table name

// Connect to server and select database.
mysql_connect("$host", "$db_username", "$db_password")or die("cannot connect to database"); 
mysql_select_db("$db_name")or die("cannot select DB");
$con=mysql_connect("$host","$db_username","$db_password","$db_name");

echo "test";
//retrieve messages
$query = mysql_query($con, "SELECT * FROM messages WHERE key='Josh'") or       die(mysql_error());
echo "test2";
$row = mysql_fetch_assoc($query);
$msg = $row['msg_array'];

echo "test3";
echo mysql_num_rows($query);
echo $msg;

?>

Only "test//test2//test3" is returned, nothing more.

Here's my database Here's mdatabase

I've been trying to simply get something returned for hours and am getting a tad annoyed. I've had loads of errors as I've been changing it but this time, nothing's returned.

This should work as I've returned data multiple times and know how to do it from scratch. All of my other sites work properly and I haven't updated phpMyAdmin either.

It's probably a small error but it's really bugging me Any help would be appreciated, thanks.

Edit

I placed

error_reporting(E_ALL);
ini_set('display_errors',1);

at the top of my code and it returned:

test//test2// Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\Users\Josh\SkyDrive\Default Sync Folder\Websites\IM\global.php on line 22 test3 Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\Users\Josh\SkyDrive\Default Sync Folder\Websites\IM\global.php on line 26

Marc Delisle
  • 8,760
  • 3
  • 27
  • 29
Josh-Mason
  • 229
  • 3
  • 7
  • 21
  • 1
    You're using `mysql_query` wrong - the SQL statement is the first parameter, and the database connection is the second, optional, parameter. It's `mysqli_query()` that has the parameters in that order. – andrewsi Aug 09 '13 at 14:45

2 Answers2

2
$con=mysql_connect("$host","$db_username","$db_password","$db_name");

looks extremely strange

I suppose a lot PHP errors but it seems you are unable to see them.
Add these lines at the top of your code

error_reporting(E_ALL);
ini_set('display_errors',1);

and then check the manual page for the every function name you see in the error message

Besides that, your main problem is that

you are using an outdated library

Here is how it has to be:

<?php
error_reporting(E_ALL);
ini_set('display_errors',1);

// connect (better to be moved into included file)
$dsn = "mysql:host=localhost;dbname=messenger;charset=utf8";
$opt = array(
    PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
);
$db = new PDO($dsn,"Josh", "password", $opt);

// getting data
$stm = $db->query("SELECT * FROM messages WHERE `key`='Josh'");
$row = $stm->fetch();
$msg = $row['msg_array'];
Community
  • 1
  • 1
Your Common Sense
  • 154,967
  • 38
  • 205
  • 325
  • using variables in that way is not good but also not uncommon, especially with novice php programmers - they'll figure it out one day – Ivan Hušnjak Aug 09 '13 at 14:48
  • Sorry, the double quotes are from me messing around earlier and I forgot to remove them. I put the error reporting on; look at the edit to see what it showed. – Josh-Mason Aug 09 '13 at 14:56
  • @Josh-Mason it is not quotes your problem but **extra connect with wrong syntax** – Your Common Sense Aug 09 '13 at 14:57
  • But I use this exact method with other projects ans it works fine – Josh-Mason Aug 09 '13 at 15:04
  • The method you suggested gives a long error including "uncaught exception", "incorrect syntax"...etc – Josh-Mason Aug 09 '13 at 15:05
  • @Josh-Mason that's what is GREAT with this method exactly. Error messages intended to be read and fixed, mind you. They have **meaning.** Whatever, you have to use it anyway. – Your Common Sense Aug 09 '13 at 15:08
  • Yes but there's no point in going for a method I don't understand. I'm simply a 16 year-old who loves to code and hasn't had the experience you have with the more complex methods. I don't like to use things I can't understand. – Josh-Mason Aug 09 '13 at 15:10
  • @Josh-Mason the problem is: **you don't understand your current method either**. Which you shouldn't use anyway: Even a 16 year-old could understand [**the red alert box on the manual page**](http://php.net/manual/en/function.mysql-query.php) – Your Common Sense Aug 09 '13 at 15:13
  • @Josh-Mason by the way, I just fixed in my code error from **your** query. Remember - even if you don't understand the error message, it is better to have it rather than not. – Your Common Sense Aug 09 '13 at 15:20
  • It works now, so, thanks. I will have to look into the method you have given and use it more in the future. Thanks again for your help:) – Josh-Mason Aug 09 '13 at 15:26
0
mysql_connect("$host", "$db_username", "$db_password")or die("cannot connect to database"); 

to

mysql_connect($host, $db_username, $db_password)or die("cannot connect to database"); 

These are no strings which has to be escaped, they are variables.

Also, add:

error_reporting(E_ALL);
ini_set("display_erorrs","on");

Also, when using a single mysql_connection, you can remove the:

$con=mysql_connect(...);

It automaticly selects then the current one.

D. Schalla
  • 645
  • 4
  • 9