Reputation: 11
For some reason my SQL won't connect any idea's? I used functions to connect to the SQl but having some trouble because it tells me the the $row_results gives back no data.
function db_connect() {
$db_connect = mysql_connect($db_host, $db_user, $db_pass);
if (!$db_connect)
{
die('Can not connect: ' . mysql_error());
}
mysql_select_db($db_name, $db_connect);
}
function db_close() {
mysql_close($db_connect);
}
function db_select($table_name, $table_where) {
$row_result = mysql_query("SELECT * FROM '$table_name' WHERE email='$table_where'");
$row_get = mysql_fetch_array($row_result);
$row_count = mysql_num_rows($row_result);
}
db_connect();
db_select("attachments", "[email protected]");
die($row_get['email']);
db_close();
Upvotes: 0
Views: 200
Reputation: 1
Document
$connection->close(); ?> this better
Upvotes: 0
Reputation: 3713
you've got a problem with your variables.
outside the function db_select
the variable $row_get does not exist !
If you want to send your result in a new variable, yo've got to use return like that :
function db_select($table_name, $table_where,$connection) {
$row_result = mysqli_query($connection,"SELECT * FROM $table_name WHERE email='".mysqli_real_escape_string($table_where).'");
$row_get = mysqli_fetch_array($row_result);
$row_count = mysqil_num_rows($row_result);
return array($row_get,$row_count);
}
$var = db_select("attachments", "[email protected]", db_connect());
echo $var[0]['email'];
Upvotes: 0
Reputation: 91744
One problem I can see, is this function:
function db_select($table_name, $table_where) {
$row_result = mysql_query("SELECT * FROM '$table_name' WHERE email='$table_where'");
$row_get = mysql_fetch_array($row_result);
$row_count = mysql_num_rows($row_result);
}
You are not returning anything from it.
You could start by using for example:
function db_select($table_name, $table_where) {
$row_result = mysql_query("SELECT * FROM '$table_name' WHERE email='$table_where'");
$row_get = mysql_fetch_array($row_result);
$row_count = mysql_num_rows($row_result);
return array($row_count, $row_get);
}
And you really need to switch to prepared statements with bound variables in PDO or mysql to avoid the sql injection problem you have now.
Upvotes: 2
Reputation: 32262
In this line you have quotes around the table name.
$row_result = mysql_query("SELECT * FROM '$table_name' WHERE email='$table_where'");
Do not put quotes around the table name. Running mysql_error()
after the select will likely spit back an error relating to this.
Also, mysql_*
are being deprecated. You should be using mysqli_*
or PDO.
Upvotes: 0
Reputation: 1
Try This:
$db_config = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
For later versions of PHP its is recommended to use the improved MySQL library.
Upvotes: 0
Reputation: 242
change this
if (!$db_connect)
{
die('Can\'t connect: ' . mysql_error());
}
Upvotes: 0