duanao4503 2013-03-26 20:02
浏览 70
已采纳

OOPHP MySQLi参数化查询不起作用?

I have been trying to make this work for a couple of days now. I am wanting to evolve my knowledge of PHP and database security and am looking to use OOPHP and paramterized queries. I figured I would start off by simply connecting to the database and performing a select and displaying the results. I know the paramaterized query works on its own, but when I put it inside a class and call it, it won't grab the results and display them. Here is my code I've been working on:

<?php

class connect
    {
    public static function run()
        {
        $mysqli = new mysqli("*****", "*****", "*****");

        $db = "sales";
        mysqli_select_db($db);

        if(!$mysqli)
            {
            printf("Connection Failed: %s
", mysqli_connect_error());
            }
            else
                {
                echo("MariaDB Connection Successfull!!!");
                }
        }
    }

//call_user_func(array($className, 'run'));

class contacts
    {
    public function select()
        {
        if($stmt = $mysqli->prepare("SELECT CON_ID, CON_FNAME, CON_LNAME, CON_EMAIL FROM contacts"))
            {
            $stmt->execute();
            $stmt->bind_result($id,$fname,$lname,$email);

            echo("<table align='center' width='40%'");
            echo("<tr>");
            echo("<td align='center' width='25%'>Contact ID</td>");
            echo("<td align='center' width='25%'>First Name</td>");
            echo("<td align='center' width='25%'>Last Name</td>");
            echo("<td align='center' width='25%'>Email Address</td>");
            echo("</tr>");

            while($stmt->fetch())
                {
                echo("<tr>");
                echo("<td align='center' width='25%'>");
                printf("%s",$this->$id);
                echo("</td>");
                echo("<td align='center' width='25%'>");
                printf("%s",$this->$fname);
                echo("</td>");
                echo("<td align='center' width='25%'>");
                printf("%s",$this->$lname);
                echo("</td>");
                echo("<td align='center' width='25%'>");
                printf("%s",$this->$email);
                echo("</td>");
                echo("</tr>");
                }
            printf("</table>");
            $stmt->close();
            }
            else
                {
                printf("Prepared Statement Error: %s
", $mysqli->error());
                }
        }
    }

$connect = "connect";
$connect::run();
$contact = "contacts";
$contact::select();
?>

When I run the page, the only output I receive is "MariaDB Connection Successful!!!", but it doesn't seem to run the select() within the class "contacts". I am looking to learn better ways of coding with PHP, so if there is a better way, I would love to learn it!

I would be very grateful for any help that anyone can give.

  • 写回答

1条回答 默认 最新

  • dongshi2458 2013-03-26 20:30
    关注

    There are 2 major problems with your code

    1. You are using mysqli instead of PDO, making your goal ten times harder to reach
    2. You are trying to do all at once. It is not way to go. Try to make yourself familiar with API, run some queries using just plain text code. Then combine these code into functions. And only then start for the class.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘