-1

Recently I've bought webhosting at names.co.uk and I'm trying to set up something simple which will display the name from a table named Team if the id = 1. This is my code

<?php 
$q = "SELECT * FROM `Team` WHERE id =1";

$result = mysql_query($q);

echo '<br />Query is send';
    echo '<br />Result is true';
    $row = mysql_fetch_array($result);
    echo '<br />tryed fetching row';
    if ($row === FALSE) {
        echo '<br />$row is not false.';
        $name = $row['name'];
        echo '<br />$name now is "' . $name . '"';
    }
    else {
        echo( mysql_error());
    }

echo $name;
    ?>

This is the output:

Query is send Result is true tryed fetching rowNo such file or directory

UPDATE:

I have changed to msqli:

$q = "SELECT * FROM `Team` WHERE id =1";

$result = mysqli_query($q);

echo '<br />Query is send';
    echo '<br />Result is true';
    $row = mysqli_fetch_array($result);
    echo '<br />tryed fetching row';
    if ($row !== FALSE) {
        echo '<br />$row is not false.';
        $name = $row['name'];
        echo '<br />$name now is "' . $name . '"';
    }
    else {
        echo( mysqli_error());
    }

echo $name;

and now I'm getting this output:

Query is send Result is true tryed fetching row $row is not false. $name now is ""

2 Answers2

0

You need to fist establish a connection. For example: $connection = mysqli_connect($servername, $username, $password);.

See this link on how to use MySQLi: https://www.w3schools.com/PHP/php_mysql_connect.asp (but note that w3schools is a bad resource, with outdated information and bad practices - I'm only linking to it because this tutorial is basic and clear).

Be sure to check later, if you still haven't, on how to properly sanitize your queries. See this, for example: How can I prevent SQL injection in PHP?

flen
  • 1,905
  • 1
  • 21
  • 44
-3

Use function:

$result = mysqli_query($q);

mysql_query() have been deprecated in PHP7 onwards.