0

Im creating a forum where user can create a poll by giving the question and options. Then I send these to a database using Ajax and PHP and create a table with the name PollTable0. And as new ones get added the number increases s.a. "PollTable1", "PollTable2" etc. I get these numbers by counting the number of tables in the database. My code looks like this:

<?php
if (isset($_POST['NewPoll'])) {
    $PollArray = $_POST['NewPoll'];
}
$con = mysqli_connect("localhost", "root", "123", "polldatabase") or die("DIE");

$getnumber = "SELECT COUNT(*) FROM information_schema.TABLES
            WHERE TABLE_SCHEMA='polldatabase'";
$number = mysqli_query($con, $getnumber);

$sql = "CREATE TABLE PollTable" . $number . " (" . $PollArray[0] . "VarChar(255))";
mysqli_query($con, $sql);

for ($i = 1; $i < count($PollArray); $i++) {
$target =   "ALTER TABLE PollTable" . $number . " ADD " . $PollArray[i] . " VARCHAR(255)";
mysqli_query($con, $target);
}
echo "Thank you for submitting your poll";
mysqli_close($con);
?>

Now the problem is $number is an I believe sql result object. Therefore when adding up the string with $number it gives the error:

Object of class mysqli_result could not be converted to string

If I am right, is there a way of converting result objects to strings. I have tried strval() and some other methods, which didnt work. If I am wrong, what do you think is the reason I am getting this error? Thanks in advance.

3 Answers 3

1

You should add after line:

$number = mysqli_query($con, $getnumber);

the following lines:

$number = mysqli_fetch_row($number);
$number = $number[0];

mysqli_query only query database but if you want to fetch results you need to use one of the functions: mysli_fetch_row, mysqli_fetch_array, mysqli_fetch_assoc

Sign up to request clarification or add additional context in comments.

Comments

1

The issue is here

$number = mysqli_query($con, $getnumber);

Mysqli::query will return

Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object. For other successful queries mysqli_query() will return TRUE.

And you are using $number in the next line and hence its issuing the error.

You will need to fetch the result before using it

http://www.php.net/manual/en/mysqli-result.fetch-assoc.php

Something as

$getnumber = "SELECT COUNT(*) as total FROM information_schema.TABLES
            WHERE TABLE_SCHEMA='polldatabase'";
$result = mysqli_query($con, $getnumber);
$data = mysqli_fetch_assoc($result);
$number = $data['total'];

Comments

0

You need to change

$getnumber = "SELECT COUNT(*) FROM information_schema.TABLES
            WHERE TABLE_SCHEMA='polldatabase'";
$number = mysqli_query($con, $getnumber);

to

$getnumber = "SELECT COUNT(*) as number FROM information_schema.TABLES
            WHERE TABLE_SCHEMA='polldatabase'";
$query = mysqli_query($con, $getnumber);
$result = mysqli_fetch_object($query);
$number = $result->number;

4 Comments

But you also need to fetch result. Don't you?
@MarcinNabiałek what do you mean?
Its not giving that error anymore but apparently I still have mistakes... Thank you though
Undefined property: mysqli_result::$number This is on the line I added: $number = $result->number;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.