Getting a simple php script to display results

Codes here !

Moderators: egami, macek, gesf

Post Reply
nickphp
New php-forum User
New php-forum User
Posts: 9
Joined: Mon Sep 04, 2017 11:44 pm

Wed Sep 06, 2017 8:46 am

<?php
$servername = "localhost";
$username = "";
$password = "";
$dbname = "";

// Create connection
$conn = new mysqli($servername, $username, $password, $dname);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";

$sql = "SELECT * from MyGuests";
$result = $conn->query($sql);

//Output data of each row
while($row = $result->fetch_assoc())
{
echo $row["firstname"];
}

$conn->close;
?>


Can not get the script to display anything from the table MyGuests

Please help me to het this to work

User avatar
hyper
php-forum Active User
php-forum Active User
Posts: 433
Joined: Mon Feb 22, 2016 5:52 pm

Wed Sep 06, 2017 4:28 pm

Do you get the
Connected successfully
message?

User avatar
Strider64
php-forum Active User
php-forum Active User
Posts: 307
Joined: Sat Mar 23, 2013 8:24 am

Thu Sep 07, 2017 7:50 am

Do you have error reporting turned on?

Code: Select all

/* Turn on error reporting */
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);

if (filter_input(INPUT_SERVER, 'SERVER_NAME', FILTER_SANITIZE_URL) == "localhost") {
    error_reporting(-1); // -1 = on || 0 = off
} else {
    error_reporting(0); // -1 = on || 0 = off
    // Prevents javascript XSS attacks aimed to steal the session ID
    ini_set('session.cookie_httponly', 1);

// **PREVENTING SESSION FIXATION**
// Session ID cannot be passed through URLs
    ini_set('session.use_only_cookies', 1);

// Uses a secure connection (HTTPS) if possible
    ini_set('session.cookie_secure', 1);
}
Sorry for the extra code, but I just copied it from a project that I'm working on.
Life is a fig newton of your imagination! https://www.pepster.com/index

nickphp
New php-forum User
New php-forum User
Posts: 9
Joined: Mon Sep 04, 2017 11:44 pm

Fri Sep 08, 2017 5:07 am

Thank you for getting back to me, i will copy and paste the error reporting section of your code
in to mine and see what happens

nickphp
New php-forum User
New php-forum User
Posts: 9
Joined: Mon Sep 04, 2017 11:44 pm

Fri Sep 08, 2017 7:47 am

I have pasted some of your code this bit:-

/* Turn on error reporting */
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);


The code now looks like this:-

/* Turn on error reporting */
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);

// Create connection
$conn = new mysqli($servername, $username, $password);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
{
echo "Connected successfully";
}

$sql = "SELECT * FROM TABLE 2";
$result = $conn->query($sql);
$nrows = mysql_num_rows($result);
//echo "$nrows \n";
//while($row = $result->fetch_assoc())
//{
// $row['COL 1']. "<br>";
//}
?>

The error i get is this:-

Connected successfully
Fatal error: Uncaught Error: Call to undefined function mysql_num_rows() in /home/abilityi/public_html/Ability_sites/mysql_connect2.php:23 Stack trace: #0 {main} thrown in /home/abilityi/public_html/Ability_sites/mysql_connect2.php on line 23


Line 23 is line where i am trying to use the row function to count the rows

User avatar
hyper
php-forum Active User
php-forum Active User
Posts: 433
Joined: Mon Feb 22, 2016 5:52 pm

Sun Sep 10, 2017 10:07 am

You're mixing mysqli and mysql statetements up
Call to undefined function mysql_num_rows()
It might be better to convert your code into PDO, it is better, easier and would lead to fewer types of these problems.

User avatar
hyper
php-forum Active User
php-forum Active User
Posts: 433
Joined: Mon Feb 22, 2016 5:52 pm

Sun Sep 10, 2017 10:10 am

And post your code in the reply box using the </> button, it makes it easier to read the code.

Code: Select all

/* Turn on error reporting */
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);

// Create connection
$conn = new mysqli($servername, $username, $password);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
{
echo "Connected successfully";
}

$sql = "SELECT * FROM TABLE 2";
$result = $conn->query($sql);
$nrows = mysql_num_rows($result);
//echo "$nrows \n";
//while($row = $result->fetch_assoc())
//{
// $row['COL 1']. "<br>";
//}
?>

nickphp
New php-forum User
New php-forum User
Posts: 9
Joined: Mon Sep 04, 2017 11:44 pm

Mon Sep 11, 2017 4:27 am

mysqli

As far as I understand mysqli

Stands for mysqli improved

So it is best to use mysli

Am i correct?

nickphp
New php-forum User
New php-forum User
Posts: 9
Joined: Mon Sep 04, 2017 11:44 pm

Mon Sep 11, 2017 7:27 am

I have created a new script all using mysqli

<?php
$servername = "localhost";
$username = "abilityi_test1";
$password = "~ws4K+4=kc]V";
$database ="ability_test";

// Create connection
$conn = new mysqli($servername, $username, $password);

//check connection

if (!conn)
{
echo "Unable to connect to database";
}
else
{
echo"connected to database";
}
mysqli_set_charset($conn, 'utf8');
if (!mysqli_set_charset($conn, 'utf8'))
{
echo "Unable to set database encoding";
}
else
{
echo "<br>" . "Encoding for Database set";
}
mysqli_select_db($conn, $database);
if (!mysqli_select_db)
{
echo "<br>" . "Could not connect to Database";
}
else
{
echo "<br>" . "Connected to Database";
}
$result = mysqli_query($conn, 'SELECT * FROM TABLE_2');
if (!result)
{
echo "<br>" . "Query string empty";
}
else
{
echo "<br>" . "Query string not empty";
}
while ($row = mysqli_fetch_assoc($result))
{
echo "<br>" . $row["COL 1"];
}
?>


This does not give me any errors

This is the output from the script in the browser window
connected to database
Encoding for Database set
Connected to Database
Query sring not empty

I am trying to display from TABLE_2 of the database COL 1

Just want to get it to display data from the table, all the checks indicate the results from
database TABLE_2 are in $result

So surely as it loops through each row it should display the value in COL 1 in that record, row?

User avatar
hyper
php-forum Active User
php-forum Active User
Posts: 433
Joined: Mon Feb 22, 2016 5:52 pm

Mon Sep 11, 2017 7:44 am

As far as I understand mysqli

Stands for mysqli improved
MySQLi stands for MySQL improved (there is a difference, I'm not being pedantic).
So it is best to use MySQLi
it is not best to use MySQLi (although it is better than MySQL)
Am i correct?
No - you are not correct in my opinion for the reasons detailed below.

It was an improvement over MySQL and introduced what was supposed to be a transitional interface for object orientated database access for when MySQL was dropped from the PHP specification in favour of PDO (which has now happened). MySQLi provides an interface to MySQL with improved security.

It's completely up to you if you want to change to PDO, it provides an interface for [nearly] all databases and so if you wanted to change database, it would be a much easier process - it's by no means perfect and neither is MySQLi which would give you all sorts of headaches if you wanted to change database.

---------------------------------

Why can't you use the </> code button for code?

Why have you removed error reporting?

Both the above will help get answers along with posting any errors in your post.

nickphp
New php-forum User
New php-forum User
Posts: 9
Joined: Mon Sep 04, 2017 11:44 pm

Mon Sep 11, 2017 11:41 pm

Sorry i did not put it back in started the code from scratch pretty much will drop it back in

nickphp
New php-forum User
New php-forum User
Posts: 9
Joined: Mon Sep 04, 2017 11:44 pm

Mon Sep 11, 2017 11:42 pm

Sorry for being a bit dim, what do you mean by:-

Why can't you use the </> code button for code?

nickphp
New php-forum User
New php-forum User
Posts: 9
Joined: Mon Sep 04, 2017 11:44 pm

Tue Sep 12, 2017 1:18 am

I have added the error reporting code back in now, the code is now this:-

<?php
$servername = "localhost";
$username = "abilityi_test1";
$password = "~ws4K+4=kc]V";
$database ="ability_test";

/* Turn on error reporting */
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);

// Create connection
$conn = new mysqli($servername, $username, $password);

//check connection

if (!conn)
{
echo "Unable to connect to database";
}
else
{
echo"connected";
}
mysqli_set_charset($conn, 'utf8');
if (!mysqli_set_charset($conn, 'utf8'))
{
echo "Unable to set database encoding";
}
else
{
echo "<br>" . "Encoding for Database set";
}
mysqli_select_db($conn, $database);
if (!mysqli_select_db)
{
echo "<br>" . "Connected";
}
else
{
echo "<br>" . "Connected to Database";
}
$result = mysqli_query($conn, 'SELECT * FROM TABLE 2');
if (!result)
{
echo "<br>" . "Query string empty";
}
else
{
echo "<br>" . "Query string not empty";
}
while ($row = mysqli_fetch_assoc($result))
{
echo "<br>" . $row["COL 1"];
}
?>


This is the information I am getting back form the browser now:-

connected
Encoding for Database set
Connected to Database
Query string not empty
Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in /home/abilityi/public_html/Ability_sites/mysql_connect.php on line 51

User avatar
hyper
php-forum Active User
php-forum Active User
Posts: 433
Joined: Mon Feb 22, 2016 5:52 pm

Tue Sep 12, 2017 11:18 am

Look, very carefully at these lines (42 & 43):

Code: Select all

$result = mysqli_query($conn, 'SELECT * FROM TABLE 2');
if (!result)

When you use the </> button in the "Post a reply" box, the code is formatted like it is above.

nickphp
New php-forum User
New php-forum User
Posts: 9
Joined: Mon Sep 04, 2017 11:44 pm

Wed Sep 13, 2017 1:37 am

It is displaying data from a table:-

<?php
$servername = "localhost";
$username = "abilityi_test1";
$password = "~ws4K+4=kc]V";
$database ="abilityi_myDB";

/* Turn on error reporting */
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);

// Create connection
$conn = new mysqli($servername, $username, $password);

//check connection

if (!$conn)
{
echo "Unable to connect to database";
}
else
{
echo"connected";
}
mysqli_set_charset($conn, 'utf8');
if (!mysqli_set_charset($conn, 'utf8'))
{
echo "Unable to set database encoding";
}
else
{
echo "<br>" . "Encoding for Database set";
}
mysqli_select_db($conn, $database);
if (!mysqli_select_db)
{
echo "<br>" . "Connected";
}
else
{
echo "<br>" . "Connected to Database" . "<br>";
}
$query_string = "SELECT * FROM MyGuests";
echo $query_string;
$result = mysqli_query($conn, $query_string);
if (!result)
{
echo "<br>" . "Query string empty";
}
else
{
echo "<br>" . "Query string not empty" . "<br>";
}
$row=mysqli_fetch_assoc($result);
printf ($row["firstname"]);
//while $row = mysqli_fetch_assoc($result))
//{
// echo $row["firstname"] . "<br>";
//}
?>

result from above script is:-

connected
Encoding for Database set
Connected to Database
SELECT * FROM MyGuests
Query string not empty
John

if i change the code to this i get an error page not working and nothing?

<?php
$servername = "localhost";
$username = "abilityi_test1";
$password = "~ws4K+4=kc]V";
$database ="abilityi_myDB";

/* Turn on error reporting */
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);

// Create connection
$conn = new mysqli($servername, $username, $password);

//check connection

if (!$conn)
{
echo "Unable to connect to database";
}
else
{
echo"connected";
}
mysqli_set_charset($conn, 'utf8');
if (!mysqli_set_charset($conn, 'utf8'))
{
echo "Unable to set database encoding";
}
else
{
echo "<br>" . "Encoding for Database set";
}
mysqli_select_db($conn, $database);
if (!mysqli_select_db)
{
echo "<br>" . "Connected";
}
else
{
echo "<br>" . "Connected to Database" . "<br>";
}
$query_string = "SELECT * FROM MyGuests";
echo $query_string;
$result = mysqli_query($conn, $query_string);
if (!result)
{
echo "<br>" . "Query string empty";
}
else
{
echo "<br>" . "Query string not empty" . "<br>";
}
$row=mysqli_fetch_assoc($result);
//printf ($row["firstname"]);
while $row = mysqli_fetch_assoc($result))
{
echo $row["firstname"] . "<br>";
}
?>


Just need to get the loop bit to work

Amy hrlp apprectiated please

Post Reply