View Full Version : Having issue with SELECT * FROM
samsam
11-12-2015, 02:22 AM
<?php
$connection = mysql_connect("xxxxxx", "xxxxxx", "xxxxx","xxxxxxx") or die ("Couldn't connect to server.");
$db = mysql_select_db("persons", $connection) or die ("Couldn't select database.");
$search=$_POST['search'];
$data = 'SELECT * FROM `table_name` WHERE `ID` = "'.$search.'"';
$query = mysql_query($data) or die("Couldn't execute query. ". mysql_error());
$data2 = mysql_fetch_array($query);
?>
still getting this ERR Couldn't execute query. No such file or directory
Beverleyh
11-12-2015, 06:46 AM
Try removing the weird apostrophes around `table_name`and `ID`and check that table_name is correct
james438
11-13-2015, 04:35 PM
Try echoing $data and see what query is being sent.
EDIT:
The backtics you are using are fine. They are known as identifiers and allow you to use odd names for tables and columns that are outside of the normal [a-zA-Z_0-9] character list. I personally don't use them. For more on identifiers see this:
http://dev.mysql.com/doc/refman/5.0/en/identifiers.html
It appears the MySQL website has been updated finally and is actually user friendly now. I was beginning to wonder if they would ever update their website ;)
In your case if you echoed your query it would look like this:
SELECT * FROM `table_name` WHERE `ID` = "search"
where search is whatever your search term or phrase is. Notice that your quotes are being displayed as well. I have not tested it, but the additional quotes you have added should not be necessary. I would just use
$data = "SELECT * FROM table_name WHERE ID = $search";
You are also using the very deprecated mysql as opposed to the standard mysqli
Here is one of my typical queries using this method:
$get_addresses = "SELECT ID FROM news WHERE ID = $ID";
$get_addresses_res = mysqli_query($conn,$get_addresses);
$add_info = mysqli_fetch_array($get_addresses_res);
$ID = $add_info['ID'];
To read more about it go here: http://dev.mysql.com/downloads/connector/php-mysqlnd/
There is also one or two threads here you should take a look at:
http://www.dynamicdrive.com/forums/showthread.php?71716-mysql_*-is-being-deprecated-!
In fact I'm thinking the above thread should be stickied to the MySQL forum.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.