Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

please convert my php old fashion  to mysqli or PDO

Posted on 2015-02-04
16
Medium Priority
?
71 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
  • 2
  • +1
16 Comments
 
LVL 32

Accepted Solution

by:
Daniel Wilson earned 1600 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 31

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 1600 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
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 

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 1600 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 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 400 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
 
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 1600 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 111

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By, Vadim Tkachenko. In this article we’ll look at ClickHouse on its one year anniversary.
In this article, I’ll talk about multi-threaded slave statistics printed in MySQL error log file.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

610 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