Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8685635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:37:46+00:00 2026-06-12T22:37:46+00:00

Hi I have been learning php from this book PHP Solutions Dynamic Web Design

  • 0

Hi I have been learning php from this book PHP Solutions Dynamic Web Design Made Easy and gotten to the part where I have to work with mysqli api for databases.After writing a connection function and running the script I get this error:

enter image description here

This is my code:

function dbConnect($usertype , $connectionType = 'mysqli'){
    $host = 'localhost';
    $db = 'phpsols';
    if($usertype == 'read'){
        $user = 'psread';
        $pwd = 'Aleczandru1989';
    }elseif($usertype == 'write'){
        $user = 'aleczandru';
        $pwd = 'Aleczandru1989';
    }else{
        exit('Unrecognized type');
    }

    if($connectionType == 'mysqli'){
        return new mysqli($host , $user , $pwd , $db) or die ('Cannot open database');
    }else{
        try{
            return new PDO("mysql:host=$host;dbname=$db", $user, $pwd); 
        }  catch (PDOException $e){
            echo 'Cannot connect to database';
            exit;
        }
    }
}

$conn = dbConnect('read');
$sql = 'SELECT * FROM images';
$result = $conn->query($sql) or die(mysqli_error()); //Line 5
$numRows = $result->num_rows;

Line 5 in this case refers to $result = $conn->query($sql) or die(mysqli_error());.
What Am I doing wrong here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T22:37:47+00:00Added an answer on June 12, 2026 at 10:37 pm

    The $conn object you are attempting to create with dbConnect(‘read’); fails. If you would do a var_dump($conn); it probably shows it is not what you aspect it to be. The error is actually describing what is wrong. You try to access the query function with ‘->query(..’ on $conn. But $conn has to be an object reference that actually has the query function. The points where this object will be created are:

    return new mysqli($host , $user , $pwd , $db)
    

    and

    return new PDO("mysql:host=$host;dbname=$db", $user, $pwd); 
    

    Since you are showing a different error then

    or die ('Cannot open database');
    

    My guess it is actually gong wrong at

    return new PDO("mysql:host=$host;dbname=$db", $user, $pwd);
    

    And you will catch the exception. But the echo statement is not visible anymore due to the fatal error. You will have to do some debugging there!

    I have no experience with PDO, but construction of the object seems ok. (but can this help you out: https://www.php.net/manual/en/class.pdo.php#84751) If the construction is ok, than check if your database engine is actually running 🙂 ?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently learning PHP and MySQL and I have been working from a basic
I have been learning syntax for PHP and practicing it. I come from a
I have been learning Hibernate for the past few weeks, I have gotten most
I have been learning python for some time now. While starting this learning python
Bear with me, I have been only learning PHP for only a few weeks,
I have been recently learning some OOP PHP and seem to have run into
I have been trying to learn about classes in PHP and part of my
I am learning Javascript, I have been using PHP for about 10 years so
I've been considering switching from using PHP to Ruby on Rails for my web
I have been learning PHP for some time now and I wanted one clarification.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.