Solved

please convert my php old fashion  to mysqli or PDO

Posted on 2015-02-04
16
45 Views
Last Modified: 2016-05-27
hello i try to convert my old fashion warning php to smtg proper



connect.php

<?php // Hote

$dbhost		="localhost";
// Nom d'utilisateur
$dblogin	="root"; 
// Password utilisateur
$dbpasspord	="";
// Nom de votre base de données
$database	="portfolio"; 
// Password administrateur pour la zone d'administration
$passadmin	="admin"; 

//if I put the following line without comment the aff_image.php script doesnt work anymore
// $link = mysqli_connect("myhost","myuser","mypassw","mybd") or die("Error " . mysqli_error($link)); 



mysql_connect($dbhost,$dblogin,$dbpasspord) or die("Impossible de se connecter à la base de données !"); 
mysql_select_db("$database") or die("Impossible de selectionner la base !"); 
mysql_query("SET NAMES 'utf8'"); 

Open in new window

<?php include('connec.php');





function affiche($src,$lang,$appel_fnc,$type,$limite){



$nombre = 5;  /* on on va afficher 5 résultats par page*/

if ($lang == "fr") {$infos="plus d'infos";}

else $infos="more informations";

              



$requete = mysql_query('SELECT *  FROM image WHERE s_cat= '.$type.' ') OR die('Erreur de la requête images');

$ntotal = mysql_num_rows($requete);







$requete2 = mysql_query('SELECT *  FROM image WHERE s_cat= '.$type.'  limit '.$limite.','.$nombre.' ')OR die('Erreur de la requête images');





$iCpt = 1;



while($resultat = mysql_fetch_assoc($requete2))

     { 

    $sClassName = 'imagette';

    if($iCpt === 1) {$sClassName = 'imagette2'; }

    ++$iCpt;

    echo '<a href="#" title="'.$infos.'"><img class="'.$sClassName.'" src='.$resultat['chemin'].' onmouseover="javascript:enlarge(this)" border="0" alt="'.$resultat['chemin'].'"  /></a>';

     }



$limitesuivante = $limite + $nombre;

$limiteprecedente = $limite - $nombre;



   echo '</div> <div id="barre_defil">';

switch ($appel_fnc) {



 /* on on va afficher 5 résultats par page*/

    case 1:

    

        if($limite != 0) {

    echo '<a href="/'.$src.'/'.$lang.'/'.$limiteprecedente.'/0/0/0"><img src="/fnc/prev.gif" border="0" alt="" class="fleche"></a>';

}



if($limitesuivante < $ntotal) {

    echo '<a href="/'.$src.'/'.$lang.'/'.$limitesuivante.'/0/0/0"><img src="/fnc/next.gif" border="0" alt="suivant" class="fleche"></a>';

}

        break;

    case 2:

          if($limite != 0) {

    echo '<a href="/'.$src.'/'.$lang.'/0/'.$limiteprecedente.'/0/0"><img src="/fnc/prev.gif" border="0" alt="" class="fleche"></a>';

}





if($limitesuivante < $ntotal) {

    echo '<a href="/'.$src.'/'.$lang.'/0/'.$limitesuivante.'/0/0"><img src="/fnc/next.gif" border="0" alt="suivant" class="fleche"></a>';

}

        break;

    case 3:

   if($limite != 0) {

    echo '<a href="/'.$src.'/'.$limiteprecedente.'"><img src="/fnc/prev.gif" border="0" alt="precedent" class="fleche"></a>';

}





if($limitesuivante < $ntotal) {

    echo '<a href="appel8.php?limite='.$limitesuivante.'"><img src="/fnc/next.gif" border="0" alt="suivant" class="fleche"></a>';

}   

   break;     

}   





   echo ' </div> ';

}







?>

Open in new window

0
Comment
Question by:jerrrrry
  • 7
  • 5
  • 2
  • +1
16 Comments
 
LVL 32

Accepted Solution

by:
Daniel Wilson earned 400 total points
ID: 40589211
OK, I'm working from this documentation.

$link = new mysqli("myhost","myuser","mypassw","mybd");
if ($link->connect_errno)  die("Error " . $link->connect_errno );

$link->query("SET NAMES 'utf8'"); 

Open in new window

0
 
LVL 30

Expert Comment

by:Marco Gasi
ID: 40589214
I can suggest you to read this: http://www.sitepoint.com/migrate-from-the-mysql-extension-to-pdo/ It can help you to migrate your code to PDO, the new de facto standard layer for myql.
0
 
LVL 32

Assisted Solution

by:Daniel Wilson
Daniel Wilson earned 400 total points
ID: 40589277
For your 2nd code block, I assume you want a parameterized query to help you avoid SQL injection.  That's one of the major draws of mysqli.

So ... lines 21-23 translate like this:

$stmt = $link->prepare('SELECT *  FROM image WHERE s_cat=  ?');
$stmt->bind_param("s", $type);
$stmt->execute();

$stmt->store_results();
$ntotal = $stmt->$stmt->num_rows;

Open in new window


http://php.net/manual/en/mysqli.prepare.php
http://php.net/manual/en/mysqli-result.num-rows.php
http://php.net/manual/en/mysqli-stmt.execute.php
http://foundationphp.com/blog/2011/09/24/using-num_rows-with-a-mysqli-prepared-statement/
0
 

Author Comment

by:jerrrrry
ID: 40589387
how can i translate this?
$requete2 = mysql_query('SELECT *  FROM image WHERE s_cat= '.$type.'  limit '.$limite.','.$nombre.' ')OR die('Erreur de la requête images');





$iCpt = 1;



while($resultat = mysql_fetch_assoc($requete2))

Open in new window



tks
$requete2 = mysql_query('SELECT *  FROM image WHERE s_cat= '.$type.'  limit '.$limite.','.$nombre.' ')OR die('Erreur de la requête images');





$iCpt = 1;



while($resultat = mysql_fetch_assoc($requete2))

Open in new window

$requete2 = mysql_query('SELECT *  FROM image WHERE s_cat= '.$type.'  limit '.$limite.','.$nombre.' ')OR die('Erreur de la requête images');





$iCpt = 1;



while($resultat = mysql_fetch_assoc($requete2))

Open in new window

0
 
LVL 32

Assisted Solution

by:Daniel Wilson
Daniel Wilson earned 400 total points
ID: 40589460
http://php.net/manual/en/mysqli-stmt.bind-param.php
http://php.net/manual/en/mysqli-result.fetch-assoc.php

$requete2 = $link->prepare('SELECT *  FROM image WHERE s_cat= ? limit ?, ?');
$requete2->bind_param("sdd", $type, $limite, $nombre);
$requete2->execute();
$requete2->store_results();

$iCpt = 1;

while ($resultat = $requete2->fetch_assoc()){

}

Open in new window

0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 100 total points
ID: 40590830
There is an article here at E-E that addresses exactly this issue.  It maps the familiar but obsolete MySQL extension to the currently supported versions.  Please read it over and post back here if you still have any questions.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html
0
 

Author Comment

by:jerrrrry
ID: 40590978
hello i have little problem...

) Notice: Undefined variable: link in C:\wamp\www\aff_image.php on line 18
Call Stack
#      Time      Memory      Function      Location
1      0.0000      145552      {main}( )      ..\web.php:0
2      0.0100      166384      affiche( )      ..\web.php:229

( ! ) Fatal error: Call to a member function prepare() on a non-object in C:\wamp\www\aff_image.php on line 18
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 32

Expert Comment

by:Daniel Wilson
ID: 40590997
That is a GREAT article, Ray!
0
 
LVL 32

Expert Comment

by:Daniel Wilson
ID: 40591000
Jerrrrry, please post the code at least through line 18 that breaks.
0
 

Author Comment

by:jerrrrry
ID: 40591011
<?php include('connec.php');





function affiche($src,$lang,$appel_fnc,$type,$limite){



$nombre = 5;  /* on on va afficher 5 résultats par page*/

if ($lang == "fr") {$infos="plus d'infos";}

else $infos="more informations";

              
$stmt = $link->prepare('SELECT *  FROM image WHERE s_cat=  ?');
$stmt->bind_param("s", $type);
$stmt->execute();

$stmt->store_results();
$ntotal = $stmt->$stmt->num_rows;


 





$requete2 = $link->prepare('SELECT *  FROM image WHERE s_cat= ? limit ?, ?');
$requete2->bind_param("sdd", $type, $limite, $nombre);
$requete2->execute();
$requete2->store_results();

$iCpt = 1;

while ($resultat = $requete2->fetch_assoc()){




     

    $sClassName = 'imagette';

    if($iCpt === 1) {$sClassName = 'imagette2'; }

    ++$iCpt;

    echo '<a href="#" title="'.$infos.'"><img class="'.$sClassName.'" src='.$resultat['chemin'].' onmouseover="javascript:enlarge(this)" border="0" alt="'.$resultat['chemin'].'"  /></a>';

     }



$limitesuivante = $limite + $nombre;

$limiteprecedente = $limite - $nombre;



   echo '</div> <div id="barre_defil">';

switch ($appel_fnc) {



 /* on on va afficher 5 résultats par page*/

    case 1:

    

        if($limite != 0) {

    echo '<a href="/'.$src.'/'.$lang.'/'.$limiteprecedente.'/0/0/0"><img src="/fnc/prev.gif" border="0" alt="" class="fleche"></a>';

}



if($limitesuivante < $ntotal) {

    echo '<a href="/'.$src.'/'.$lang.'/'.$limitesuivante.'/0/0/0"><img src="/fnc/next.gif" border="0" alt="suivant" class="fleche"></a>';

}

        break;

    case 2:

          if($limite != 0) {

    echo '<a href="/'.$src.'/'.$lang.'/0/'.$limiteprecedente.'/0/0"><img src="/fnc/prev.gif" border="0" alt="" class="fleche"></a>';

}





if($limitesuivante < $ntotal) {

    echo '<a href="/'.$src.'/'.$lang.'/0/'.$limitesuivante.'/0/0"><img src="/fnc/next.gif" border="0" alt="suivant" class="fleche"></a>';

}

        break;

    case 3:

   if($limite != 0) {

    echo '<a href="/'.$src.'/'.$limiteprecedente.'"><img src="/fnc/prev.gif" border="0" alt="precedent" class="fleche"></a>';

}





if($limitesuivante < $ntotal) {

    echo '<a href="appel8.php?limite='.$limitesuivante.'"><img src="/fnc/next.gif" border="0" alt="suivant" class="fleche"></a>';

}   

   break;     

}   





   echo ' </div> ';

}







?>

Open in new window

connec.php
//$dbhost		="localhost";
// Nom d'utilisateur
//$dblogin	="root"; 
// Password utilisateur
//$dbpasspord	="";
// Nom de votre base de données
//$database	="portfolio"; 
// Password administrateur pour la zone d'administration
//$passadmin	="admin"; 

//if I put the following line without comment the aff_image.php script doesnt work anymore
 $link = mysqli_connect("localhost","root","","portfolio") or die("Error " . mysqli_error($link)); 



//mysql_connect($dbhost,$dblogin,$dbpasspord) or die("Impossible de se connecter à la base de données !"); 
//mysql_select_db("$database") or die("Impossible de selectionner la base !"); 

//mysql_query("SET NAMES 'utf8'"); 

    

?>

Open in new window

thanks
0
 
LVL 32

Expert Comment

by:Daniel Wilson
ID: 40592117
Incorporate the first block of code I gave you to get your connection.

Have you read the article Ray posted?  It really helps!
0
 

Author Comment

by:jerrrrry
ID: 40592238
hello i try with what u gave me but it doesn't work....
there is a lot to read i haven't finished
thanks
0
 
LVL 32

Assisted Solution

by:Daniel Wilson
Daniel Wilson earned 400 total points
ID: 40592340
OK, you're trying to use $link as a global connection, right?  But in your function, $link isn't defined.  

Somewhere between lines 7 and 18 put:


global $link;

Open in new window

0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40592701
@Daniel, thanks!
0
 

Author Comment

by:jerrrrry
ID: 40593015
ok now i have  Fatal error: Call to undefined method mysqli_stmt::store_results() in C:\wamp\www\aff_image.php on line 22

<?php include('connec.php');





function affiche($src,$lang,$appel_fnc,$type,$limite){

global $link;

$nombre = 5;  /* on on va afficher 5 résultats par page*/

if ($lang == "fr") {$infos="plus d'infos";}

else $infos="more informations";

              
$stmt = $link->prepare('SELECT *  FROM image WHERE s_cat=  ?');
$stmt->bind_param("s", $type);
$stmt->execute();

$stmt->store_results();
$ntotal = $stmt->$stmt->num_rows;


 





$requete2 = $link->prepare('SELECT *  FROM image WHERE s_cat= ? limit ?, ?');
$requete2->bind_param("sdd", $type, $limite, $nombre);
$requete2->execute();
$requete2->store_results();

$iCpt = 1;

while ($resultat = $requete2->fetch_assoc()){




     

    $sClassName = 'imagette';

    if($iCpt === 1) {$sClassName = 'imagette2'; }

    ++$iCpt;

    echo '<a href="#" title="'.$infos.'"><img class="'.$sClassName.'" src='.$resultat['chemin'].' onmouseover="javascript:enlarge(this)" border="0" alt="'.$resultat['chemin'].'"  /></a>';

     }



$limitesuivante = $limite + $nombre;

$limiteprecedente = $limite - $nombre;



   echo '</div> <div id="barre_defil">';

switch ($appel_fnc) {



 /* on on va afficher 5 résultats par page*/

    case 1:

    

        if($limite != 0) {

    echo '<a href="/'.$src.'/'.$lang.'/'.$limiteprecedente.'/0/0/0"><img src="/fnc/prev.gif" border="0" alt="" class="fleche"></a>';

}



if($limitesuivante < $ntotal) {

    echo '<a href="/'.$src.'/'.$lang.'/'.$limitesuivante.'/0/0/0"><img src="/fnc/next.gif" border="0" alt="suivant" class="fleche"></a>';

}

        break;

    case 2:

          if($limite != 0) {

    echo '<a href="/'.$src.'/'.$lang.'/0/'.$limiteprecedente.'/0/0"><img src="/fnc/prev.gif" border="0" alt="" class="fleche"></a>';

}





if($limitesuivante < $ntotal) {

    echo '<a href="/'.$src.'/'.$lang.'/0/'.$limitesuivante.'/0/0"><img src="/fnc/next.gif" border="0" alt="suivant" class="fleche"></a>';

}

        break;

    case 3:

   if($limite != 0) {

    echo '<a href="/'.$src.'/'.$limiteprecedente.'"><img src="/fnc/prev.gif" border="0" alt="precedent" class="fleche"></a>';

}





if($limitesuivante < $ntotal) {

    echo '<a href="appel8.php?limite='.$limitesuivante.'"><img src="/fnc/next.gif" border="0" alt="suivant" class="fleche"></a>';

}   

   break;     

}   





   echo ' </div> ';

}







?>

Open in new window

thanks
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now