0

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();
4
  • 4
    Please don't use mysql_* functions in new code. They were removed from PHP 7.0.0 in 2015. Instead, use prepared statements via PDO or MySQLi. See Why shouldn't I use mysql_* functions in PHP? for more information. Commented Nov 19, 2012 at 21:04
  • The syntax highlighting shows that your script has an error. Commented Nov 19, 2012 at 21:05
  • Do not use mqsql_* functions. Use mysqli or PDO. There is a syntax error on die('Can't connect: ' . mysql_error()); where you need to escape the apostrophe in "Can\'t" Commented Nov 19, 2012 at 21:05
  • You need to replace die('Can't connect: ' with die("Can't connect: " for starters... Commented Nov 19, 2012 at 21:05

6 Answers 6

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Ahh yes, variable scoping. Nice catch.
0

change this

if (!$db_connect)
{
    die('Can\'t connect: ' . mysql_error());
}

2 Comments

I know that wasn't the problem through I only changed it to can't for stackoverflow.
Try to print your query and put it directly in sql. do you have any result?
0

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.

Comments

0

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.

Comments

0

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'];

Comments

0
Document

List of Employees


ID First Name Last Name Email Phone Address Action connect_error) { die("Connection failed: " . $connection->connect_error); } // read all row from database table $sql = "SELECT * FROM employees"; $result = $connection->query($sql); if (!$result) { die("Invalid query: " . $connection->error); } // read data of each row while($row = $result->fetch_assoc()) { echo " " . $row["id"] . " " . $row["first_name"] . " " . $row["last_name"] . " " . $row["email"] . " " . $row["phone"] . " " . $row["address"] . " Update Delete "; }

$connection->close(); ?> this better

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.