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.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?