Using MySQLi to connect

Codes here !

Moderators: egami, macek, gesf

Post Reply
KramsonKrate
New php-forum User
New php-forum User
Posts: 5
Joined: Wed Apr 28, 2021 4:48 pm

Thu Apr 29, 2021 6:27 am

I've just updated to PHP 5.6, (don't ask) and several things are depreciated from 5.3 which I had been using. I know this is simple, but I suppose I want to think it through out loud, and request suggestions for making it better.

My old code looked something like this:

Code: Select all

 <?php 
	 	 
		 include('req.php');
		 include('bank.php');
		 
	 $query="SELECT * FROM xx WHERE xx IN ('xx')";
		
		  $resource=mysql_query($query,$link);
		  echo "
		<table id=\"space\" class=\"sortable\" align=\"center\" border=\"0\" width=\"95%\">
		<tr>
		<td><b>Column 1</b></td><td><b>Column 2</b></td><td><b>Column 3</b></td><td><b>Column 4</b></td><td><b>Column 5</b></td><td><b>Column 6</b></td><td><b>Column 3</b></td></tr> ";
while($result=mysql_fetch_array($resource))
	{ 
	echo "<tr><td>".$result[1]."  </td><td>".$result[8]."</td><td>".$result[15]."</td><td>".$result[21]."</td><td>".$result[17]."</td><td>".$result[16]."</td><td><a href='/pages/things.php?result=$result[24]'>Link</a></td></tr>";
	} echo "</table>";
	 ?></p>
Where bank.php looks like this:

Code: Select all

<?php
$link = mysql_connect($dbhost, $dbuser, $dbpass) or die                      ('Error connecting to mysql');
mysql_select_db($dbname);
?>
and req.php looks something like this:

Code: Select all

<?php
$dbhost = 'db.server.com';
$dbuser = 'xx';
$dbpass = 'xx';
$dbname = 'xx';
?>
I think the right answer is to change bank.php to this:

Code: Select all

<?php
$link = mysqli_connect($dbhost, $dbuser, $dbpass) or die                      ('Error connecting to mysql');
mysqli_database($dbname);
?>
But I'm not sure how to phrase the query. Can anyone help here? Many thanks in advance!
KramsonKrate
New php-forum User
New php-forum User
Posts: 5
Joined: Wed Apr 28, 2021 4:48 pm

Mon May 03, 2021 4:10 am

^ bump

Can anyone please help with the above issue? Kind regards!
User avatar
hyper
php-forum GURU
php-forum GURU
Posts: 1206
Joined: Mon Feb 22, 2016 5:52 pm

Tue May 04, 2021 11:42 am

You may not like this, but don't take it the wrong way.
I'm not sure how to phrase the query
Whatever SQL you have, will work with very few tweaks depending on how badly it was written in the first place.

I'm not sure about everyone else, but, I looked at your code and thought 'this code that you have shown us is very old and out of date - it's not something that will easily be fixed with a simple line change'.

The best answer that I can give is to learn current HTML, CSS, PHP and SQL / database. Your HTML looks to be version 3 with an attempt at using HTML 4 (the addition of CSS) although I'll concede you may have thought that you were using HTML 4 but were using transitional in much the same way you are now trying the use MySQLi.

The PHP that you have shown, to my knowledge will work in PHP 5.6, although not recommended and I haven't checked.

Long story or not, you need to ditch the newly(?) upgraded PHP 5.6 in favour of PHP 7 in any case.

For database access in PHP, you should use PDO in place of MySQL / MySQLi and use it in the correct way - by this I mean that you should be using prepared statements rather than replacing mysql_xxx for whatever else will work.

The changes introduced to HTML, PHP and SQL were done for a reason.
KramsonKrate
New php-forum User
New php-forum User
Posts: 5
Joined: Wed Apr 28, 2021 4:48 pm

Wed May 05, 2021 5:49 am

I very much appreciate your reply - it confirms my suspicions. I built this application in 2005, and it has worked perfectly since then. I built it to learn basic LAMP stack concepts, and it was an awesome project for what it was.

My host has 5.6 by default in my service, so I need to move to a new host.

I will do as you say, and educate myself on these concepts so I can rebuild this application.

Thank you, and kindly.
User avatar
hyper
php-forum GURU
php-forum GURU
Posts: 1206
Joined: Mon Feb 22, 2016 5:52 pm

Wed May 05, 2021 7:35 am

Most of what you already know still applies, I think CSS will be the biggest change for you.

HTML provides the structure of the document - write it as though you are writing for a screen reader
CSS supplies how it looks on different devices

The main mind shift will be changing how you present variables to a query - it's called preparing and seems quite daunting to begin with.

Don't believe everything that you read in tutorials as some (many) are rather rubbish, you can still learn from them though.

Although I don't think that this is a beginners tutorial, it looks as though you should be able to cope with it as you do have some knowledge of SQL to begin with, and will hopefully have a flying start.
KramsonKrate
New php-forum User
New php-forum User
Posts: 5
Joined: Wed Apr 28, 2021 4:48 pm

Thu May 06, 2021 6:32 am

This is incredibly kind of you - and I will endeavor to do as you suggest. I do have strong experience with very basic SQL, and have done a good deal of css troubleshooting, but have never started from scratch with css.

I have two kids and a full-time+ job, but this application is precious to me, and I've wanted to update it with a responsive design for over a decade.

It's an application that displays books in my collection (8000+), and related details and statistics. I want to build out the SQL to handle views and objects, but haven't had the time. Instead of doing that with the time I had when I built it, I learned how to build a search engine from scratch, and that was an awesome learning experience that I will never regret.

But the vision is to be able to use a mobile device to check if I have a book/edition in my collection. The main interface in my home is intended to be a lovely, old-school card catalogue, which butterflys-open when you pull the center drawers to reveal a tablet stand which runs this application alone.

I will take this work to another thread here in this excellent forum, and post my progress. Kind regards.
KramsonKrate
New php-forum User
New php-forum User
Posts: 5
Joined: Wed Apr 28, 2021 4:48 pm

Thu May 06, 2021 6:34 am

Actually - the main vision for this project originally was to use this use-case as a reason to build, fail, and learn! I learned a lot, but there are clearly lots of new things to learn, here, over a decade later.
Post Reply