Jump to content

error converting mysqli to pdo


Recommended Posts

hello, i really have no time to code it for you. i need to mention that my last post needs to be adjusted by you. the query needs to be changed from columns to all if you wish to fill the variables correctly.

so
 

$query = 'SELECT convoy_name, convoy_organizer FROM convoys where id = :getId';

needs to be

$query = 'SELECT * FROM convoys where id = :getId';

you take things to literally and i have no time to be perfect. i need to go for now.

so this is not correctly formed. I wish i knew how to describe the variables accurately but i do not know how to do so. let's try it this way and see if it makes sense:

you need to define a way to connect to the database using pdo. thus $bdd = new PDO (); you can name this variable whatever you want to name it. $80s_rocker_still_rockin_but_missing_me_long_hair = new PDO(). get it?

then you need a query, which can be called whatever you want it to be called. such as $qry, $query, $scorpions_bad_boys_running_wild. who cares about the name. The concept is important here: $jodunno_nuthin = 'SELECT * FROM convoy_part WHERE user_convoy= :I"; you are actually going to 'prepare' this query for execution.

then you use another variable named anything that you want to represent the database actions being performed. $s2, $start, $kix_blow_my_fuse or whatever.

$gnr_nighttrain = new PDO("bla blah", more-blah , blah);

$jodunno_nuthin = 'SELECT * FROM convoy_part WHERE user_convoy= :I";

$load_the_cannons = $gnr_nighttrain->prepare($jodunno_nuthin);

$load_the_cannons->execute(array(':I' => $_GET['id']));

$db_is_shooting_back = $load_the_cannons->fetch();

put it all together in one sensical operation. so let's light the cannons and fire:

$query = "SELECT * FROM convoy_part WHERE user_convoy= :I';
$start = $bdd->prepare($query);
$start->execute(array(':I' => $_GET['id']));

then perform your fetch. think of a dog chasin' a stick: fetch boy!
$result = $start->fetch();

naming variables really doesn't matter when you are practicing code. understanding variables is 'sehr wichtig'. keep it all sensical, logical and easy to understand when you look at the code 5 years from now. The idea is to understand what $bdd represents and what $query represents, etc. you are often times getting lost and mixing variable names as if you don't understand what is happening. stay focused!

Best wishes.

8 hours ago, jodunno said:

so
 


$query = 'SELECT convoy_name, convoy_organizer FROM convoys where id = :getId';

needs to be


$query = 'SELECT * FROM convoys where id = :getId';

 

That is one of the worse pieces of advice I have seen in these forums for a long time.

@Endrick, ignore what @jodunno said in the above quote and specify the columns your query needs - don't use "SELECT * ". One day it will come back to bite you.

Consider this scenario...

Joe has written a script which creates a register of all student names, and uses "SELECT * FROM student" (with the excuse that he needs all the columns anyway) and iterates through the results. He then takes a holiday and, while he is away, things change. A colleague of his adds a new "dissertation" column and an image BLOB column to the student table. This contains the text of each student's 10,000 word final dissertation and a photo of the student. His register program still works but a query that took milliseconds now takes an age to run. The register doesn't need the dissertation or image data but, nevertheless, is dragging them down from the database for every student.

  • Like 1
On 8/1/2020 at 9:53 PM, jodunno said:

I strongly urge you to create a practice file. You have to integrate your code to the pdo format. You implemented pdo but you still have old mysqli statements left in the code which need to be removed. I wouldn't loop at all if you are only fetching all columns in one row. a loop is necessary if you need all columns from many rows.

xampp is nice and easy to install. set up a simple database and create a php file to practice pdo. like so:


database name: mytestdb
table name: convoys
columns: id, user_id, convoy_name, convoy_organizer

$host = 'localhost';
$yourdbnamevar = 'mytestdb';
$your_db_username = 'root';
$your_db_password = '';
$attributes = array(
  PDO::ATTR_EMULATE_PREPARES => false,
  PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
);
$fake_id = 1;
$connection = new PDO("mysql:host=$host; dbname=$yourdbnamevar; charset=utf8mb4", $your_db_username, $your_db_password, $attributes);
$query = 'SELECT convoy_name, convoy_organizer FROM convoys where id = :id';
$start = $connection->prepare($query);
$start->execute(array(':id' => $fake_id)); 
$result = $start->fetch();

echo $result['convoy_name'] . '<br>' . $result['convoy_organizer'];

Best wishes.

advice allready given. read entire thread b4 trolling.

On 8/2/2020 at 2:01 PM, jodunno said:

so this is not correctly formed. I wish i knew how to describe the variables accurately but i do not know how to do so. let's try it this way and see if it makes sense:

you need to define a way to connect to the database using pdo. thus $bdd = new PDO (); you can name this variable whatever you want to name it. $80s_rocker_still_rockin_but_missing_me_long_hair = new PDO(). get it?

then you need a query, which can be called whatever you want it to be called. such as $qry, $query, $scorpions_bad_boys_running_wild. who cares about the name. The concept is important here: $jodunno_nuthin = 'SELECT * FROM convoy_part WHERE user_convoy= :I"; you are actually going to 'prepare' this query for execution.

then you use another variable named anything that you want to represent the database actions being performed. $s2, $start, $kix_blow_my_fuse or whatever.

$gnr_nighttrain = new PDO("bla blah", more-blah , blah);

$jodunno_nuthin = 'SELECT * FROM convoy_part WHERE user_convoy= :I";

$load_the_cannons = $gnr_nighttrain->prepare($jodunno_nuthin);

$load_the_cannons->execute(array(':I' => $_GET['id']));

$db_is_shooting_back = $load_the_cannons->fetch();

put it all together in one sensical operation. so let's light the cannons and fire:


$query = "SELECT * FROM convoy_part WHERE user_convoy= :I';
$start = $bdd->prepare($query);
$start->execute(array(':I' => $_GET['id']));

then perform your fetch. think of a dog chasin' a stick: fetch boy!
$result2 = $start->fetch();

naming variables really doesn't matter when you are practicing code. understanding variables is 'sehr wichtig'. keep it all sensical, logical and easy to understand when you look at the code 5 years from now. The idea is to understand what $bdd represents and what $query represents, etc. you are often times getting lost and mixing variable names as if you don't understand what is happening. stay focused!

Best wishes.

 

I think you get me wrong, the list has a problem getting the data from the one in the quote.

Code of List:

 <table class="table">
                                <thead>
                                    <tr>
                                    <th scope="col">#</th>
                                    <th scope="col">User</th>
                                    <th scope="col">Datum</th>
           
                                    <th scope="col"><a class="btn btn-primary" href="convoy_user.php?id=<?php echo $result2['user_convoy']; ?>&action=part" role="button">Teilnehmen</a></th>
                                    </tr>
                                </thead>
                            <tbody>
                        <?php
                        while($result2 = $start->fetch()){
                        ?>
                        <tr>
                            <th scope="row"><?php echo $result2['id']; ?></th>
                            <td><?php echo $result2['name']; ?></td>
                            <td><?php echo $result2['date']; ?></td>
                        </tr>
                     <?php
                        }
                        ?>
                  </tbody>
                </table>

 

On 8/3/2020 at 3:01 PM, Endrick said:

 

I think you get me wrong, the list has a problem getting the data from the one in the quote.

Code of List:


 <table class="table">
                                <thead>
                                    <tr>
                                    <th scope="col">#</th>
                                    <th scope="col">User</th>
                                    <th scope="col">Datum</th>
           
                                    <th scope="col"><a class="btn btn-primary" href="convoy_user.php?id=<?php echo $result2['user_convoy']; ?>&action=part" role="button">Teilnehmen</a></th>
                                    </tr>
                                </thead>
                            <tbody>
                        <?php
                        while($result2 = $start->fetch()){
                        ?>
                        <tr>
                            <th scope="row"><?php echo $result2['id']; ?></th>
                            <td><?php echo $result2['name']; ?></td>
                            <td><?php echo $result2['date']; ?></td>
                        </tr>
                     <?php
                        }
                        ?>
                  </tbody>
                </table>

 

Do anyone have an idea how can i fix this

On 8/2/2020 at 9:32 PM, jodunno said:

table name: convoys

columns: id, user_id, convoy_name, convoy_organizer

then

<?php echo $result2['user_convoy']; ?>

and

<td><?php echo $result2['name']; ?></td>
<td><?php echo $result2['date']; ?></td>

Where are those values (user_convoy, name and date) supposed to come from?

$query = 'SELECT * FROM convoy_part WHERE user_convoy= :I';
$start = $bdd->prepare($query);
$start->execute(array(':I' => $_GET['id']));
$result2 = $start->fetch();
 <div class="col-lg-12">
                    <div class="block margin-bottom-sm">
                        <div class="table-responsive"> 
                            <table class="table">
                                <thead>
                                    <tr>
                                    <th scope="col">#</th>
                                    <th scope="col">User</th>
                                    <th scope="col">Datum</th>
           
                                    <th scope="col"><a class="btn btn-primary" href="convoy_user.php?id=<?php echo $result2['user_convoy']; ?>&action=part" role="button">Teilnehmen</a></th>
                                    </tr>
                                </thead>
                            <tbody>
                        <?php
                        while($result2 = $start->fetch()){
                        ?>
                        <tr>
                            <th scope="row"><?php echo $result2['id']; ?></th>
                            <td><?php echo $result2['name']; ?></td>
                            <td><?php echo $result2['date']; ?></td>
                        </tr>
                     <?php
                        }
                        ?>
                  </tbody>
                </table>

 

Edited by Guest
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.